'use strict';

const {
  FEEDBACK_LIST_CATCE_KEY,
} = require('../../../../config/redisKey');

exports.create = async ctx => {
  ctx.validate({
    name: {
      type: 'string',
      min: 1,
      max: 20,
    },
    icon: {
      type: 'string',
    },
    phone: {
      required: false,
      type: 'string',
    },
    qq: {
      required: false,
      type: 'string',
    },
    wx: {
      required: false,
      type: 'string',
    },
    isShow: {
      required: false,
      type: 'boolean',
      default: false,
    },
    date: {
      required: false,
      type: 'date',
      default: new Date(),
    },
    content: {
      type: 'string',
      max: 140,
      min: 1,
    },
  });
  await ctx.service.feedback.add();
  ctx.sendSuccess();
};

exports.index = async ctx => {
  const resData = await ctx.getListCache(FEEDBACK_LIST_CATCE_KEY, async () => {
    ctx.w_validate_page();
    const data = await Promise.all([ctx.service.feedback.list(), ctx.service.feedback.count()]);
    return {
      list: data[0],
      total: data[1],
      pageSize: ctx.query.pageSize,
      pageNum: ctx.query.pageNum,
    };
  })
  ctx.sendSuccess(resData);
};
